from aiogram import F
from aiogram.enums import ContentType
from aiogram_dialog import Dialog, StartMode, Window
from aiogram_dialog.widgets.input import MessageInput, TextInput
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select, Start
from aiogram_dialog.widgets.media import DynamicMedia
from aiogram_dialog.widgets.text import Format

from poker_bot.tg_bot.dialogs.extras import copy_start_data_to_ctx
from poker_bot.tg_bot.dialogs.getters.payments import (
    payment_method_getter,
    payment_methods_getter,
)
from poker_bot.tg_bot.dialogs.reusable_handlers import set_payment_method_id
from poker_bot.tg_bot.dialogs.widgets import LocaleText
from poker_bot.tg_bot.states import ClientSG, CreateDepositSG

from .handlers import create_or_update_deposit, process_invoice_screenshot

select_payment_method_window = Window(
    LocaleText("select-payment-method-msg"),
    ScrollingGroup(
        Select(
            Format("{item.currency}"),
            "payment_method_sel",
            lambda method: method.id,
            "payment_methods",
            on_click=set_payment_method_id,
        ),
        width=2,
        height=3,
        id="useritemssel",
        hide_on_single_page=True,
    ),
    Start(
        LocaleText("back-btn"),
        id="back_btn",
        state=ClientSG.start,
        mode=StartMode.RESET_STACK,
    ),
    state=CreateDepositSG.select_payment_method,
    getter=payment_methods_getter,
)

select_amount_window = Window(
    LocaleText("enter-payment-amount", currency_symbol="{currency_symbol}"),
    TextInput("payment_amount", str, on_success=create_or_update_deposit),
    Back(LocaleText("back-btn")),
    state=CreateDepositSG.select_amount,
    getter=payment_method_getter,
)

send_invoice_screenshot_window = Window(
    DynamicMedia("payment_image", when=F["payment_image"]),
    LocaleText(
        "send-invoice-screenshot-msg",
        summ="{dialog_data[deposit_summ]}",
        currency="{currency_symbol}",
        payment_address="{payment_address}",
    ),
    MessageInput(
        process_invoice_screenshot,
        content_types=[ContentType.PHOTO, ContentType.DOCUMENT],
    ),
    Back(
        LocaleText("back-btn"),
    ),
    state=CreateDepositSG.send_invoice_screenshot,
    getter=payment_method_getter,
)

create_deposit_dialog = Dialog(
    select_payment_method_window,
    select_amount_window,
    send_invoice_screenshot_window,
    on_start=copy_start_data_to_ctx,
)
